const mysql = require('mysql');

const { host, port, user, pswd, database } = {
    host: '192.168.44.128',
    port: 32769,
    user: 'root',
    pswd: '123456',
    database: 'xxx'
};

// 连接数据库
const db = mysql.createConnection({
    host: host,
    port: port,
    user: user,
    password: pswd,
    database: database
});

// 设置数据库连接标志变量
console.log('\n正在连接数据库。。。请稍等。。。');
db.laoyu_successful = false;

// 检测数据库是否连接成功
db.connect((err) => {
    if (err) {
        console.log(`数据库连接失败！！！！`);
        console.log(` -> host: ${host}`);
        console.log(` -> port: ${port}`);
        console.log(` -> user: ${user}`);
        console.log(` -> password: ${pswd}`);
        console.log(` -> database: ${database}`);
        process.exit(1);
    }
    console.log('数据库连接成功！  数据库进程id为: ' + db.threadId);
    db.laoyu_successful = true;
});

module.exports = db;